package com.webwork.project.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.webwork.common.dao.ColumnLimit;
import com.webwork.common.dao.DBSimpleSQL;
import com.webwork.common.dao.SqlCondition;
import com.webwork.common.dao.impl.SimpleDao;
import com.webwork.common.dataobject.Bool;
import com.webwork.project.system.dao.IQueryMenuDao;
import com.webwork.project.system.dao.IUpdateMenuDao;
import com.webwork.project.system.dataobject.SysMenu;
import com.webwork.project.system.dataobject.SysMenuQuery;

/**
 * 菜单Dao实现
 *
 */
@Repository
public class MenuDaoImpl extends SimpleDao implements IQueryMenuDao, IUpdateMenuDao {

	public MenuDaoImpl() {
		super();
		this.dbTable = "system_menu";
		dbColumns.clear();
		dbColumns.put("id", "id");
		dbColumns.put("menuCode", "menu_code");
		dbColumns.put("menuName", "menu_name");
		dbColumns.put("menuUrl", "menu_url");
		dbColumns.put("menuOrder", "menu_order");
		dbColumns.put("status", "menu_status");
		dbColumns.put("parentId", "parent_id");
		dbColumns.put("menuIcon", "menu_icon");
		dbColumns.put("comment", "comment");
	}
	
	@Override
	public ColumnLimit condition(Object obj) {
		if (!(obj instanceof SysMenuQuery))
			return new ColumnLimit();
		SysMenuQuery o = (SysMenuQuery) obj;
		ColumnLimit ret = new ColumnLimit();
		if (null != o) {
			if (o.getId() > 0) {
				ret.add(dbColumns.get("id").concat(SqlCondition.EQ));
			}
			if (Bool.NULL != o.getMenuStatus()) {
				ret.add(dbColumns.get("status").concat(SqlCondition.EQ));
			}
		}
		return ret;
	}

	@Override
	public Object[] statement(Object obj) {
		if (!(obj instanceof SysMenuQuery))
			return null;
		SysMenuQuery o = (SysMenuQuery) obj;
		List<Object> ret = new ArrayList<Object>();
		if (null != o) {
			if (o.getId() > 0) {
				ret.add(o.getId());
			}
			if (Bool.NULL != o.getMenuStatus()) {
				ret.add(o.getMenuStatus().getValue());
			}
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		SysMenu o = (SysMenu) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getId());
		objs.add(o.getMenuCode());
		objs.add(o.getMenuName());
		objs.add(o.getMenuUrl());
		objs.add(o.getMenuOrder());
		objs.add(o.getMenuStatus().getValue());
		objs.add(o.getParentId());
		objs.add(o.getMenuIcon());
		objs.add(o.getComment());
		return objs;
	}

	@Override
	public Object catchObject(ResultSet rs) throws SQLException {
		SysMenu ret = new SysMenu();
		ret.setId(rs.getInt(dbColumns.get("id")));
		ret.setMenuCode(rs.getString(dbColumns.get("menuCode")));
		ret.setMenuName(rs.getString(dbColumns.get("menuName")));
		ret.setMenuUrl(rs.getString(dbColumns.get("menuUrl")));
		ret.setMenuOrder(rs.getInt(dbColumns.get("menuOrder")));
		ret.setMenuStatus(rs.getInt(dbColumns.get("status")));
		ret.setParentId(rs.getInt(dbColumns.get("parentId")));
		ret.setMenuIcon(rs.getString(dbColumns.get("menuIcon")));
		ret.setComment(rs.getString(dbColumns.get("comment")));
		return ret;
	}
	
	@Override
	public int insert(SysMenu o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		return this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public int update(SysMenu o, int id) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("id").concat(SqlCondition.EQ));
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(id);
		return this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public int delete(int id) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("id").concat(SqlCondition.EQ));
		String sql = DBSimpleSQL.del(dbTable, limit);
		return this.getJdbcTemplate().update(sql, id);
	}

	@Override
	public List<SysMenu> query(SysMenuQuery obj, int page, int size) {
		List<SysMenu> ret = new ArrayList<SysMenu>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(obj), page, size);
		ret = this.getJdbcTemplate().query(sql, new RowMapper<SysMenu>() {
			@Override
			public SysMenu mapRow(ResultSet rs, int rowNum) throws SQLException {
				return (SysMenu) catchObject(rs);
			}
		}, this.statement(obj));
		return ret;
	}

	@Override
	public int count(SysMenuQuery obj) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(obj));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(obj), Integer.class);
	}
	
}
